local GuildWarChanllengeRender = app.mvc.createRenderClass(...)
local NewBoxTipsView = require("app.newBag.views.NewBoxTipsView")
--data =
function GuildWarChanllengeRender.create(node)
    local sprite = GuildWarChanllengeRender.extend(node or cc.CSLoader:createNode(Res.CSGuildWarChanllengeRender))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init()
    return sprite
end


function GuildWarChanllengeRender:init(data)
    self.super.init(self, data)
    self._index = 1
    self:registerViewEvent({
        {name = ViewEvent.Common_GetTaskReward, callback = handler(self, self._onRefreshBoxState)}, 
        {name = ViewEvent.CSGuildWar_ShowChallengeTips, callback = handler(self, self._onOpenTips)}
    })
    self._ui["Panel_116/icon/icon"]:setTouchEnabled(true)
    self._ui["Panel_116/icon/icon"]:addClickEventListener(function()
       
        if self._data then
            local task = UD:getTaskById(self._data)
            if task and task:isFinished() then
                TaskManager:C2S_GetTaskRewardRequest(nil, self._data)
            else
                self:_onShowTips()
            end
           
        end
    end)
end

function GuildWarChanllengeRender:setIndex(index)
    self._index = index
    local iconName = string.format("%s%s%d.png",Res.guildIconPath,"ghchest_",index)
    self._ui["Panel_116/icon/icon"]:loadTexture(iconName)
end


function GuildWarChanllengeRender:_onRefreshBoxState(eventName, type, taskId)
    local taskIdNum = tonumber(taskId or 0)
    if self._data and self._data == taskIdNum then
        self:refresh()
        UD.redPoint:checkStateByKey(RedPointConstant.CSGuildWar_Challenge)
    end
end

function GuildWarChanllengeRender:refresh()
    if not self._data then
        return
    end

    local task = UD:getTaskById(self._data)
    -- print("===========getTaskById==+>>> ", task)
    -- dump(task)
    if not task then
        local Task = require("app.public.dao.Task")
        task = Task.new()
        task:initWithId(self._data) 
    end

    self._ui["Panel_116/tlt"]:setString(task:getName())
    self._ui["Panel_116/Panel_bar/tips"]:setString(task:getContent())
    self._ui["Panel_116/icon/light"]:setVisible(task:isFinished())
    if task:isRewarded() then
        self._ui["Panel_116/icon_over"]:setVisible(true)
        self._ui["Panel_116/Panel_bar"]:setVisible(false)
    else
        self._ui["Panel_116/icon_over"]:setVisible(false)
        self._ui["Panel_116/Panel_bar"]:setVisible(true)
    end

    local conf = task:getConfig()
    local progress = task:getDefaultProgress()
    local curNum = 0
    local needNum = tonumber(conf.condition1Par2)
    if progress then
        curNum = progress.Num
        needNum = progress.NeedNum
    end
    
    local percent = math.floor(curNum * 100/needNum)
    if percent > 100 then
        percent = 100
    end
    self._ui["Panel_116/Panel_bar/num"]:setString(curNum.."/"..needNum)
    self._ui["Panel_116/Panel_bar/LoadingBar"]:setPercent(percent)
end



function GuildWarChanllengeRender:_onOpenTips(eventName,  index)
    if self._index == index then
        self:_onShowTips()
    end
end

function GuildWarChanllengeRender:_onShowTips()
    if not self._data then
        return
    end
    local task = UD:getTaskById(self._data)
    -- print("===========getTaskById==+>>> ", task)
    -- dump(task)
    if not task then
        local Task = require("app.public.dao.Task")
        task = Task.new()
        task:initWithId(self._data) 
    end
    local dataTemp = {
        name = task:getName(),
        desc = L("guild_challenge_tips1"),
        notice = "",
    }
    local defaultRewards = task:getDefaultRewards()
    local rewards = {}
    for k ,v in pairs(defaultRewards) do
        for k2, v2 in pairs(v) do
            table.insert( rewards, v2 )
        end
    end

    dataTemp.rewards = rewards
    local rarePanel = NewBoxTipsView.create()
    rarePanel:setData(dataTemp)
    UIHandler:addPopupUi(rarePanel)
    -- 点击空白区域关闭
    display.nodeRemoveForNotHit(rarePanel, function()
        rarePanel:removeFromParent()
    end)
    local worldPos = self._ui["Panel_116/icon/icon"]:convertToWorldSpaceAR(cc.p(0, 0))
    local size = rarePanel:getContentSize()
    local nodeHeight = size.height
    local nodeWidht = size.width
    -- display.adjustNodePos 会把rarePanel默认和box的最上面对齐，这里需要中间对齐，所以就手动设置坐标了
    rarePanel:setPositionX(worldPos.x - nodeWidht)
    rarePanel:setPositionY(worldPos.y - nodeHeight)
end



return GuildWarChanllengeRender